package com.hwhhhh.mygrad.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.hwhhhh.mygrad.HomeActivity;
import com.hwhhhh.mygrad.R;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * DialogFragment onCreate、 onCreateDialog、 onCreateView、 onViewCreated、 onDestroy
 */
public class ScreenDialogFragment extends DialogFragment {

    public static final String CAMPUS ="campus";
    public static final String START ="start";
    public static final String END ="end";
    private static ScreenDialogFragment screenDialogFragment;

    private View rootView;
    private LoopView loopViewCampus, loopViewStart, loopViewEnd;
    private TextView tvDialogTitle, tvDialogCancel, tvDialogConfirm;
    private final List<String> campusList = new ArrayList<String>() {{
        this.add("小营");
        this.add("健翔桥");
        this.add("清河");
    }}, timeList = new ArrayList<String>() {{
        for (int i = 1; i < 14; i++) {
            this.add(i + "");
        }
    }};
    private int campusIndex, startIndex, endIndex;
    private ScreenDialogCallback callback;

    public void setCallback(ScreenDialogCallback callback) {
        this.callback = callback;
    }

    public static ScreenDialogFragment getInstance(int campus, int start, int end) {
        if (screenDialogFragment == null) screenDialogFragment = new ScreenDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(CAMPUS, campus);
        bundle.putInt(START, start);
        bundle.putInt(END, end);
        screenDialogFragment.setArguments(bundle);
        return screenDialogFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_screen_dialog, container, false);
        initView();
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onStart() {
        WindowManager.LayoutParams attributes = getDialog().getWindow().getAttributes();
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.gravity = Gravity.BOTTOM;
        getDialog().getWindow().setAttributes(attributes);
        getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
        super.onStart();

    }

    protected void initView() {
        loopViewCampus = rootView.findViewById(R.id.loopView_campus);
        loopViewStart = rootView.findViewById(R.id.loopView_start);
        loopViewEnd = rootView.findViewById(R.id.loopView_end);
        tvDialogTitle = rootView.findViewById(R.id.tv_dialog_title);
        tvDialogCancel = rootView.findViewById(R.id.tv_dialog_cancel);
        tvDialogConfirm = rootView.findViewById(R.id.tv_dialog_confirm);
    }

    protected void initData() {

        // 获取当前位置
        campusIndex = getArguments().getInt(CAMPUS, 0);
        startIndex = getArguments().getInt(START, 0);
        endIndex = getArguments().getInt(END, 12);
        // 设置dialog标题
        tvDialogTitle.setText(getTitleText(campusIndex, startIndex, endIndex));
        loopViewCampus.setItems(campusList);
        loopViewStart.setItems(timeList);
        loopViewEnd.setItems(timeList);
        // 设置初始位置
        loopViewCampus.setInitPosition(campusIndex);
        loopViewStart.setInitPosition(startIndex);
        loopViewEnd.setInitPosition(endIndex);
        // 设置选中的字体颜色
        loopViewCampus.setCenterTextColor(Color.parseColor("#FF6200EE"));
        loopViewStart.setCenterTextColor(Color.parseColor("#FF6200EE"));
        loopViewEnd.setCenterTextColor(Color.parseColor("#FF6200EE"));
        // 设置监听事件
        loopViewCampus.setListener(index -> tvDialogTitle.setText(getTitleText(index, -1, -1)));
        loopViewStart.setListener(index -> tvDialogTitle.setText(getTitleText(-1, index, -1)));
        loopViewEnd.setListener(index -> tvDialogTitle.setText(getTitleText(-1, -1, index)));
        // 点击确定
        tvDialogConfirm.setOnClickListener(v -> {
            if (startIndex > endIndex) Toast.makeText(getContext(), "开始课程节数应大于结束课程节数！", Toast.LENGTH_LONG).show();
            else {
                ((HomeActivity)getActivity()).setCampus(campusIndex);
                ((HomeActivity)getActivity()).setStart(startIndex);
                ((HomeActivity)getActivity()).setEnd(endIndex);
                this.callback.doPositiveClick();
                screenDialogFragment.dismiss();
            }
        });
        tvDialogCancel.setOnClickListener(v -> {
            screenDialogFragment.dismiss();
        });
    }

    // 获取转盘选择的标题
    public String getTitleText(int campus, int start, int end) {
        String res;
        if (campus == -1) res = campusList.get(campusIndex) + "校区  ";
        else {
            res = campusList.get(campus) + "校区  ";
            campusIndex = campus;
        }
        if (start == -1) res += "第" + timeList.get(startIndex) + "节  至  ";
        else {
            res += "第" + timeList.get(start) + "节  至  ";
            startIndex = start;
        }
        if (end == -1) res += "第" + timeList.get(endIndex) + "节";
        else {
            res += "第" + timeList.get(end) + "节";
            endIndex = end;
        }
        return res;
    }

    public interface ScreenDialogCallback {
        void doPositiveClick();
    }
}
